package com.ccrfid.rmc6.resource.business.patrol;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.entity.business.patrol.PatrolLocationOverallEntity;
import com.ccrfid.rmc6.entity.business.patrol.PatrolLocationRecordEntity;
import com.ccrfid.rmc6.entity.business.patrol.PatrolScheduleEntity;
import com.ccrfid.rmc6.pojo.dto.patrol.PatrolCompletionRateDto;
import com.ccrfid.rmc6.pojo.dto.patrol.PatrolCompletionRateMonthlyDto;
import com.ccrfid.rmc6.service.business.patrol.PatrolService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.sql.Date;
import java.util.List;

@Path("patrol")
@Produces(MediaType.APPLICATION_JSON)
public class PatrolResource {
    @Autowired
    private PatrolService patrolService;

    //获取某段时间内的排班情况
    @GET
    @Path("schedules")
    public List<PatrolScheduleEntity> getPatrolScheduleList(@QueryParam("startDate") Date startDate,
                                                            @QueryParam("stopDate") Date stopDate) {
        return patrolService.getPatrolScheduleList(startDate, stopDate);
    }

    //获取某个排班
    @GET
    @Path("schedule/{scheduleId}")
    public PatrolScheduleEntity getPatrolSchedule(@PathParam("scheduleId") Long scheduleId) throws AppException {
        return patrolService.getPatrolSchedule(scheduleId);
    }

    //获取某天某个时段的排班信息
    @GET
    @Path("schedule")
    public PatrolScheduleEntity getPatrolSchedule(@QueryParam("dutyDate") Date dutyDate,
                                                  @QueryParam("timeRangeId") Long timeRangeId) throws AppException {
        return patrolService.getPatrolSchedule(dutyDate, timeRangeId);
    }

    //添加一个排班
    @POST
    @Path("schedule")
    @Consumes(MediaType.APPLICATION_JSON)
    public PatrolScheduleEntity createPatrolSchedule(PatrolScheduleEntity patrolSchedule) throws AppException {
        return patrolService.createPatrolSchedule(patrolSchedule);
    }

    //修改一个排班
    @PUT
    @Path("schedule/{scheduleId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public PatrolScheduleEntity updatePatrolSchedule(@PathParam("scheduleId") Long scheduleId,
                                                     PatrolScheduleEntity patrolSchedule) throws AppException {
        return patrolService.updatePatrolSchedule(scheduleId, patrolSchedule);
    }

    //删除一个排班
    @DELETE
    @Path("schedule/{scheduleId}")
    public Boolean deletePatrolSchedule(@PathParam("scheduleId") Long scheduleId) throws AppException {
        return patrolService.deletePatrolSchedule(scheduleId);
    }

    //获取某个排班下各个巡控区域的统计情况
    @GET
    @Path("schedule/{scheduleId}/locations/overall")
    public List<PatrolLocationOverallEntity> getPatrolScheduleLocationOverall(@PathParam("scheduleId") Long scheduleId) {
        return patrolService.getPatrolScheduleLocationOverall(scheduleId);
    }

    //获取某个排班下某个巡控区域的统计详细情况
    @GET
    @Path("schedule/{scheduleId}/location/{locationId}/records")
    public List<PatrolLocationRecordEntity> getPatrolScheduleLocationRecordList(@PathParam("scheduleId") Long scheduleId,
                                                                                @PathParam("locationId") Long locationId) {
        return patrolService.getPatrolScheduleLocationRecordList(scheduleId, locationId);
    }

    //获取某段时间内每天的巡控达标率
    @GET
    @Path("average-completion-rate")
    public List<PatrolCompletionRateDto> getPatrolAverageCompletionRate(@QueryParam("startDate") Date startDate,
                                                                        @QueryParam("stopDate") Date stopDate) {
        return patrolService.getPatrolAverageCompletionRate(startDate, stopDate);
    }

    //获取某段时间内每月的巡控达标率
    @GET
    @Path("average-completion-rate/monthly")
    public List<PatrolCompletionRateMonthlyDto> getPatrolAverageCompletionRateMonthly(@QueryParam("startDate") Date startDate,
                                                                                      @QueryParam("stopDate") Date stopDate) {
        return patrolService.getPatrolAverageCompletionRateMonthly(startDate, stopDate);
    }

    //巡控统计
    @POST
    @Path("statistics")
    public Boolean doStatisticPatrolSchedules() {
        patrolService.doStatisticPatrolSchedules();
        return true;
    }

    //重新统计
    @POST
    @Path("re-statistics")
    public Boolean redoStatisticPatrolSchedules(@QueryParam("startDate") Date startDate,
                                                @QueryParam("stopDate") Date stopDate) {
        patrolService.redoStatisticPatrolSchedules(startDate, stopDate);
        return true;
    }

}
